package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Comments;

public class GenericCommentDAO extends AutoKeys implements
		org.jetnuke.dao.CommentDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericCommentDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CommentModel#retrieveByPK(int)
	 */
	public Comments retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("CommentsModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Comments s = new Comments();

		if (rs.next()) {
			fillCommentsFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillCommentsFromResultSet(Comments s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillCommentsFromResultSet(Comments, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCOMMENT(rs.getString("COMMENT"));
		s.setDATE(rs.getTimestamp("DATE1"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setHOST_NAME(rs.getString("HOST_NAME"));
		s.setNAME(rs.getInt("NAME"));
		s.setPID(rs.getInt("PID"));
		s.setREASON(rs.getInt("REASON"));
		s.setSCORE(rs.getInt("SCORE"));
		s.setSID(rs.getInt("SID"));
		s.setSUBJECT(rs.getString("SUBJECT"));
		s.setTID(rs.getInt("TID"));
		s.setURL(rs.getString("URL"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillCommentsFromResultSet(Comments, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CommentModel#countAll()
	 */
	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("CommentsModel.countAll"));

		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CommentModel#addNew(org.jetnuke.entities.Comments)
	 */
	public int addNew(Comments cm) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Comments) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("CommentsModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("CommentModel.lastGeneratedCommentID"));
		initNewComment(cm, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Comments) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param cm
	 * @param p
	 */
	private void initNewComment(Comments cm, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewComment(Comments, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, cm.getCOMMENT());
		p.setTimestamp(2, new Timestamp(cm.getDATE().getTime()));
		p.setString(3, cm.getEMAIL());
		p.setString(4, cm.getHOST_NAME());
		p.setInt(5, cm.getNAME());
		p.setInt(6, cm.getPID());
		p.setInt(7, cm.getREASON());
		p.setInt(8, cm.getSCORE());
		p.setInt(9, cm.getSID());
		p.setString(10, cm.getSUBJECT());
		p.setString(11, cm.getURL());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewComment(Comments, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CommentModel#selectBySID_PID(int, int)
	 */
	public List selectBySID_PID(int id, int pid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectBySID_PID(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("CommentsModel.selectBySID_PID"));
		p.setInt(1, id);
		p.setInt(2, pid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectBySID_PID(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Comments u = new Comments();
			fillCommentsFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CommentModel#searchOrderDateDesc(java.lang.String)
	 */
	public List searchOrderDateDesc(String query) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderDateDesc(String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("CommentsModel.searchOrderDateDesc"));
		p.setString(1, query);
		p.setString(2, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderDateDesc(String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CommentModel#deleteBySID(int)
	 */
	public void deleteBySID(int sid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteBySID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("CommentsModel.deleteBySID"));
		p.setInt(1, sid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteBySID(int) - end"); //$NON-NLS-1$
		}
	}

}
